$(function(){
	var geta = ym.getQueryStringByName('a');
	console.log(geta);
	if(geta == 0){
		$('#goBackIcon').attr('href','javascript:history.go(-1)');
	}else{
		$('#goBackIcon').attr('href','askDoctor.html');
	}
	
	/*获取文章信息*/
	var articleXq=JSON.parse(window.sessionStorage.getItem("articleXq"));
	console.log(articleXq)
	var ymAnswerVO=articleXq.ymAnswerVO;
	var ymAnswerList=articleXq.ymAnswerList;
	var userid = ym.getQueryStringByName('userid');
	var exchangeId = ym.getQueryStringByName('exchangeId');
	var urls= window.location.pathname;
	var id ;
	/*查询问题详情*/
	function getXQ(){
		ym.ajax({
			url: "/Answer/queryYmAnswerListDetail",
			param:{
				typeId:articleXq.ymAnswerVO.typeId,
				answerUserId:articleXq.ymAnswerVO.answerUserId,
				answerId:articleXq.ymAnswerVO.answerId
			},
			success: function (data) {
				console.log(data.obj)
				sessionStorage.setItem('articleXq',JSON.stringify(data.obj));		
			}
		});
	}
	function addHTML(){
		var zanHTML="";
		var pinlunHTML="";
		var imgListHTML="";
		var contentFormHTML="";
		if(ymAnswerVO.praiseState===1){
			zanHTML+="<span><img src='./../img/icon-active-dianzan.png'></span>";
		}else{
			zanHTML+="<span><img src='./../img/icon-dianzan.png'></span>";
		}
		if(ymAnswerList){
			pinlunHTML="";
			for (let i = 0; i < ymAnswerList.length; i++) {
				pinlunHTML+="<div class='problem-talk content-tab'>"+
								"<div class='problem-talk-tab'>"+
									"<div class='doctor-tab-head'>"+
								  		"<div class='head-image'><img src='"+ymAnswerList[i].headimgurl+"' ></div>"+
										"<div class='person-name'>"+
											"<p class='person-name-text'>"+ymAnswerList[i].nickName+"</p>"+
											"<p class='person-reply-time'>"+ymAnswerList[i].createTimeString+"</p>"+
										"</div>"+
								 "</div>"+
								  "<div class='problem-reply'>"+
								    "<p>"+ymAnswerList[i].answerDes+"</p>"+
								  "</div>"+
								"</div>"+
							"</div>"+
						"</div>";
			}
		}else{
			pinlunHTML="";
		}
		if(ymAnswerVO.ymAttachments.length!==0||ymAnswerVO.ymAttachments!==null){
			for (var i = 0; i < ymAnswerVO.ymAttachments.length; i++) {

				imgListHTML+="<li style='width:30%;height:8rem;overflow:hidden;display:inline-block'>"+
							 "<a href='javascript:void(0)' data-magnify='gallery' data-group='g1' data-src='"+ymAnswerVO.ymAttachments[i].attachmentUrl+"' data-caption=''>"+
						     "<img src="+ymAnswerVO.ymAttachments[i].attachmentUrl+" style='width:100%;'>"+
						     "</a>"+
						     "</li>";
			}
			
		}
		else{
				imgListHTML="";
			}
		contentFormHTML+="<div class='problem-tab content-tab am-padding-bottom-0'>"+
							  "<div class='person-tab-head'>"+
							  		"<div class='head-image'><img src='"+ymAnswerVO.headimgurl+"' ></div>"+
									"<div class='person-name w65'>"+
										"<h4 class='person-name-text'>"+ymAnswerVO.nickName+"</h4>"+
										"<div class='add-problem-time font13 color93 w100'><p>"+ymAnswerVO.createTimeString+"</p></div>"+
									"</div>"+
									 "<div class='problem-dianzancount w10 am-text-right' id='dianzan' praiseState='"+ymAnswerVO.praiseState+"' answerId='"+ymAnswerVO.answerId+"'><p>"+zanHTML+"<sup> "+ymAnswerVO.praiseCountNumber+" </sup></p></div>"+
							  	 "<div class='problem-pinluncount am-margin-left-xs w10 am-text-right' id='huif'><p><span><img src='./../img/icon-pinglun.png' ></span><sup> "+ymAnswerVO.answersCount+" </sup></p></div>"+
							  "</div>"+
							  "<div class='problem-tab-desc'>"+
							    "<p>"+ymAnswerVO.answerDes+"</p>"+
							  "</div>"+
							  "<ul class='imgList picView-magnify-list' style='margin-left:3.8rem'>"+imgListHTML+"</ul>"+
							"</div>"+
							"<div>"+pinlunHTML+
							"</div>";
							
	$('.content').html(contentFormHTML);
	}
	addHTML();
	/*点赞*/
	function queryData(){
		ym.ajax({
			url: "/Answer/queryYmAnswerListDetail",
			param:{
				typeId:ymAnswerVO.typeId,
				answerId:ymAnswerVO.answerId,
				praiseType:4
			},
			success: function (data) {
				console.log(data.obj)
				sessionStorage.setItem('articleXq',JSON.stringify(data.obj));		
				//window.location.href="problemXq.html";
			}
		});
	}
	/*点赞*/
   	$('body').on('click','#dianzan',function(){
   		var praiseType = 4;
   		var praiseTypeId = $(this).attr('answerId');
   		console.log($(this).attr('praiseState'))
   		if(ymAnswerVO.praiseState == 1){
   			ym.ajax({
				url:'/communication/addPraise',
				param:{ 
					praiseTypeId:praiseTypeId,
					praiseType:praiseType
				},
				success:function(data){
					var data=JSON.parse(data);
					if(data.res ==1){
						var numbers = $('.dianzan').text();
						console.log(data.obj=='0')
						if(data.obj=='0'){
							$("#ceng").modal('open');
					   		var numbers = ymAnswerVO.praiseCountNumber-1;
					   		ymAnswerVO.praiseCountNumber=numbers;
					   		$('#info-text').html('取消点赞');
					   		setTimeout(function(){ $("#ceng").modal('close');},800);
					   		ymAnswerVO.praiseState = 0;
					   		queryData();
					   		addHTML();
					   		return;
						}
						addHTML();
					}else{
						$("#shoucang").modal('open');
			   			$('#info-text').html('取消点赞失败');
						setTimeout(function(){ $("#malert").modal('close');},800);
					}
				}
			});
   		}else if(ymAnswerVO.praiseState == 0){
   			ym.ajax({
				url:'/communication/addPraise',
				param:{ 
					praiseTypeId:praiseTypeId,
					praiseType:praiseType
				},
				success:function(data){
					var data=JSON.parse(data);
					console.log(data.obj=='1')
					if(data.res ==1){
						var numbers = $('.dianzan').text();
						console.log(data.obj=='1')
						if(data.obj=='1'){
							$("#ceng").modal('open');
					   		var numbers = ymAnswerVO.praiseCountNumber+1;
					   		console.log(numbers)
					   		ymAnswerVO.praiseCountNumber=numbers;
					   		$('#info-text').html('点赞成功');
					   		setTimeout(function(){ $("#ceng").modal('close');},800);
					   		ymAnswerVO.praiseState = 1;
					   		queryData();
					   		addHTML();
					   		return;
						}else if(data.obj=='0'){
							$("#ceng").modal('open');
					   		var numbers = ymAnswerVO.praiseCountNumber-1;
					   		console.log(numbers)
					   		ymAnswerVO.praiseCountNumber=numbers;
					   		$('#info-text').html('取消点赞');
					   		setTimeout(function(){ $("#ceng").modal('close');},800);
					   		ymAnswerVO.praiseState = 0;
					   		queryData();
					   		addHTML();
					   		return;
						}
					}else{
						$("#ceng").modal('open');
			   			$('#info-text').html('点赞失败');
						setTimeout(function(){ $("#malert").modal('close');},800);
					}
				}
			});
   		}else{
   			ym.ajax({
				url:'/communication/addPraise',
				param:{ 
					praiseTypeId:praiseTypeId,
					praiseType:praiseType
				},
				success:function(data){
					var data=JSON.parse(data);
					if(data.res ==1){
						var numbers = $('.dianzan').text();
						if(data.obj=='获得豆豆+5'){
							$("#ceng").modal('open');
					   		var numbers = ymAnswerVO.praiseCountNumber+1;
					   		console.log(numbers)
					   		ymAnswerVO.praiseCountNumber=numbers;
					   		$('#info-text').html('点赞成功，豆豆+5');
					   		setTimeout(function(){ $("#ceng").modal('close');},800);
					   		ymAnswerVO.praiseState = 1;
					   		queryData();
					   		addHTML();
					   		return;
						}
						else if(data.obj=='1'){
							$("#ceng").modal('open');
					   		var numbers = ymAnswerVO.praiseCountNumber+1;
					   		console.log(numbers)
					   		ymAnswerVO.praiseCountNumber=numbers;
					   		$('#info-text').html('点赞成功');
					   		setTimeout(function(){ $("#ceng").modal('close');},800);
					   		ymAnswerVO.praiseState = 1;
					   		queryData();
					   		addHTML();
					   		return;
						}else if(data.obj=='0'){
							$("#ceng").modal('open');
					   		var numbers = ymAnswerVO.praiseCountNumber-1;
					   		console.log(numbers)
					   		ymAnswerVO.praiseCountNumber=numbers;
					   		$('#info-text').html('取消点赞');
					   		setTimeout(function(){ $("#ceng").modal('close');},800);
					   		ymAnswerVO.praiseState = 0;
					   		queryData();
					   		addHTML();
					   		return;
						}
					}else{
						$("#ceng").modal('open');
			   			$('#info-text').html('点赞失败');
						setTimeout(function(){ $("#malert").modal('close');},800);
					}
				}
			});
   		}
   		
   })
    /*回复*/
    $('body').on('click','.problem-pinluncount',function(){
    	$('textarea').focus();
    })
    $('.fabu').click(function(){
			 var answerDes = $('textarea').val();
			 if(answerDes.length!==0){
			 ym.ajax({
				       url: "/Answer/insertYmAnswerReply",
				       type:"post",
				       param:{
				           url:urls,
				           answerDes:answerDes,
				           answerUserId:ymAnswerVO.answerUserId,
				           typeId:ymAnswerVO.typeId,
				           parentId:ymAnswerVO.answerUserId,
				           type:ymAnswerVO.type,
				           parentAnswerId:ymAnswerVO.answerId
				       },
				       success: function (data) {
				       	if(data.res==1){
				       		
				       		$('#ceng').modal('open');
				       		$('#info-text').html('回复成功！');
				       		window.setTimeout(function(){
				       			$('textarea').val("");
				       			$('#ceng').modal('close');
				       			ym.ajax({
									url: "/Answer/queryYmAnswerListDetail",
									param:{
										typeId:ymAnswerVO.typeId,
										answerId:ymAnswerVO.answerId,
										praiseType:4,
										parentAnswerId:ymAnswerVO.answerId
									},
									success: function (data) {
										articleXq=data.obj;
										ymAnswerVO=articleXq.ymAnswerVO;
										ymAnswerList=articleXq.ymAnswerList;
										addHTML();
									}
								});
				       		},500)
				       	}else{
				       		alert('添加回复失败')
				       	}
				       }
				   });
			}else{
				$('#ceng').modal('open');
				$('#info-text').html('回复内容不能为空！');
				window.setTimeout(function(){

				},500)
			}
		})
    $('[data-magnify]').Magnify({
		Toolbar: [
			'prev',
			'next',
			'rotateLeft',
			'rotateRight',
			'zoomIn',
			'actualSize',
			'zoomOut'
		],
		keyboard:true,
		draggable:true,
		movable:true,
		modalSize:[800,600],
		beforeOpen:function (obj,data) {
			console.log('beforeOpen')
		},
		opened:function (obj,data) {
			console.log('opened')
		},
		beforeClose:function (obj,data) {
			console.log('beforeClose')
		},
		closed:function (obj,data) {
			console.log('closed')
		},
		beforeChange:function (obj,data) {
			console.log('beforeChange')
		},
		changed:function (obj,data) {
			console.log('changed')
		}
	});
})